package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.PurchaseBean;
import com.project.bean.PurchaseInfoBean;
import com.project.bean.PurchaseOrderBean;
import com.project.bean.StockBean;
import com.project.mapper.IPurchaseInfoMapper;
import com.project.mapper.IPurchaseMapper;
import com.project.mapper.IPurchaseOrderMapper;
import com.project.mapper.IStockMapper;
import com.project.service.IPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 采购车业务方法
 */
@Service
@Transactional
public class PurchaseServiceImpl implements IPurchaseService {
    @Autowired
    private IPurchaseMapper purchaseMapper;
    @Autowired
    private IStockMapper stockMapper;
    @Autowired
    private IPurchaseInfoMapper purchaseInfoMapper;
    @Autowired
    private IPurchaseOrderMapper purchaseOrderMapper;
    @Override
    public List<PurchaseBean> findAll(String trueName) {
//      根据登录采购员的session中的真实姓名 查询采购车信息，返回采购车的集合
        return purchaseMapper.findAll(trueName);
    }

    @Override
    public void updateInfo(Integer id, Integer price, Integer num) {
        //通过id修改采购的价格和采购数量
        PurchaseBean purchase=purchaseMapper.selectById(id);
        purchase.setPrice(price);
        purchase.setNum(num);
        purchaseMapper.updateById(purchase);
    }

    @Override
    public void remove(Integer id) {
        // 通过采购车id移除购物车内的商品
        purchaseMapper.deleteById(id);
    }

    @Override
    public void add(String name, StockBean stockBean,Integer num) {//添加商品

        //通过商品编号和采购人的姓名判断该采购车中是否存在该商品，
        String stockId = stockBean.getStockId();//得到商品编号
        QueryWrapper<PurchaseBean> qw=new QueryWrapper<>();
        qw.eq("fk_stockId",stockId);//判断
        qw.eq("p_userName",name);
        PurchaseBean purchase=purchaseMapper.selectOne(qw);
        if (purchase!=null){
            //增加购物车内商品的数量
            purchase.setNum(purchase.getNum()+num);
            purchaseMapper.updateById(purchase);
//            System.out.println(purchase.toString());
        }else {
            //完成库存商品的添加（库存数量为0，采购价为null，销售价为null），采购车添加该商品的记录。
            QueryWrapper<StockBean> qwS=new QueryWrapper<>();
            qwS.eq("pk_stockId",stockBean.getStockId());
            StockBean stockBean1=stockMapper.selectOne(qwS);//判断库存中是否有此商品
            if (stockBean1==null){
                stockMapper.insert(stockBean);//添加商品进库存表
            }
            PurchaseBean purchaseBean=new PurchaseBean(stockBean.getStockId(),num,name);
            purchaseMapper.insert(purchaseBean);//添加商品进购物车
        }

    }

    @Override
    public void joinStock(String supplierName, String userName) {//入库
        //1、添加采购订单
        PurchaseOrderBean purchaseOrder=new PurchaseOrderBean(supplierName,userName);
        purchaseOrderMapper.insert(purchaseOrder);//添加采购订单
        // 2、按采购员姓名（登录用户姓名） 查询采购车。
        QueryWrapper<PurchaseBean> qw=new QueryWrapper<>();
        qw.eq("p_userName",userName);
        List<PurchaseBean> list=purchaseMapper.selectList(qw);
        // 3、遍历采购车，循环添加采购明细信息。循环修改库存商品的库存数量（累加）以及最新采购价
        for (int i=0;i<list.size();i++){
            PurchaseBean purchase=list.get(i);
            PurchaseInfoBean purchaseInfo=new PurchaseInfoBean
                    (purchaseOrder.getPurchaseOrderId(),purchase.getStockId(),
                            purchase.getPrice(), purchase.getNum());
            purchaseInfoMapper.insert(purchaseInfo);//添加采购明细信息
            StockBean stock=stockMapper.selectById(purchase.getStockId());//得到商品对象
            stock.setNum(stock.getNum()+ purchase.getNum());//累加库存商品数量
            stock.setInPrice(purchase.getPrice());//修改最新采购价
            stockMapper.updateById(stock);//更新
            //4、根据购物车id清空采购车
            purchaseMapper.deleteById(purchase.getPurchaseId());//
        }
//        System.out.println("采购车信息"+list.get(1));




    }
}
